package com.ss.student.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ss.common.util.JsonUtil;
import com.ss.common.util.MyToolUtil;
import com.ss.student.pojo.StudentInfo;
import com.ss.student.service.StudentService;

/**
 * 学生管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("stu/")
public class StudentController {
	@Autowired
	private StudentService service;
	/**
	 * 查询
	 * @param request
	 * @param response
	 * @param info
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryStu.do")
	public ModelAndView queryStu(HttpServletRequest request, 
	           HttpServletResponse response,StudentInfo info) throws Exception{
		List<StudentInfo> list= service.queryStu(info);
		Map<String, Object> mapResult=new HashMap<String, Object>();
		mapResult.put("list", list);
		return new ModelAndView("stu/studentManage",mapResult);
	}
	/**
	 * 根据学生id获取学生信息
	 * @param request
	 * @param response
	 * @param info
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryStuById.do")
	public ModelAndView queryStuById(HttpServletRequest request, 
	           HttpServletResponse response,StudentInfo info) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		info=service.getOneStu(info);
		mapResult.put("info",info);
		return new ModelAndView("stu/studentEdit",mapResult);
	}
	/**
	 * 根据学生id获取学生信息
	 * @param request
	 * @param response
	 * @param info
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delStu.do")
	public void delStu(HttpServletRequest request, 
	           HttpServletResponse response,StudentInfo info) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		String msg="删除失败";
		boolean r=service.delStu(info.getId());
		if(r){
			msg="删除成功";
		}
		mapResult.put("id",info.getId());
		mapResult.put("msg", msg);
		JsonUtil.outputJson(mapResult, response);
	}
	/**
	 * 保存
	 * @param request
	 * @param response
	 * @param info
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveStu.do")
	public void saveStu(HttpServletRequest request, 
	           HttpServletResponse response,StudentInfo info) throws Exception{
		String action=MyToolUtil.convertNull(request.getParameter("action"));
		String msg="保存失败";
		 Map<String, Object> mapResult=new HashMap<String, Object>();
		if(action.equals("add")){
			service.addStu(info);
			msg="保存成功";
		}else{
			boolean r =service.updateStu(info);
			if(r){
				msg="保存成功";
			}
		}
		mapResult.put("msg", msg);
		JsonUtil.outputJson(mapResult, response);
	}
}
